import dash_bootstrap_components as dbc
from dash import html, dcc, Input, Output, State
from pandas import offsets
from apps.DownData import DownData
from app import app
import dash
import time

down_data: DownData
title = html.H1("用 户 登 录", className="text-center", style={"margin": "20px"})
label_username = dbc.Label('用户名')
input_username = dbc.Input(id='input_username', value="service")
label_password = dbc.Label('密码')
input_password = dbc.Input(id="input_password", type="password")
captcha_input = dbc.Input(id="captcha_input", placeholder="请输入验证码")
# captcha_img = html.Img(id='image', src='../assets/code.jpg', height='30px',n_clicks=0)
captcha_img = html.Img(id='image', src=dash.get_asset_url(
    'code.jpg'), height='30px', n_clicks=0)
message = html.Div(id='message', children=[html.Div("  message!", className="text-secondary")],
                   style={
    "height": "30px",
    "margin-top": "20px",
    "border": "dashed",
    "border-radius": "3px",
    "border-width": "1px",
    "background": "#fff"})
submit = dbc.Button('下 载', id='submit', class_name="primary", style={
    "margin-top": "20px", "width": '100px', })

# 设置样式
layout = dbc.Container(
    id="container_login",
    children=[
        title,
        label_username,
        input_username,
        label_password,
        input_password,
        html.Div(
            [
                dbc.Row(
                    [
                        dbc.Col(
                            [captcha_input,],
                            width={"size": 9}
                        ),
                        dbc.Col([dcc.Loading(captcha_img)],                            width={"size": 1, },
                                ),
                        dbc.Col([dbc.RadioItems(options=[
                            {'label': '更新', 'value': '更新'},
                            {'label': '下载全部', 'value': '下载全部'},
                        ], value='更新', inline=True, id='choose_update')], width={'size': 2}),

                    ],
                    justify="around",
                    align="center",

                )
            ],
            style={'margin-top': '20px'},
        ),
        message,
        submit,
    ],
    style={"margin-top": "5%", },
    fluid=False
)


@app.callback(Output('message', 'children', allow_duplicate=True),
              [Input('submit', 'n_clicks'),
               State('input_username', 'value'),
               State('input_password', 'value'),
               State('captcha_input', 'value'),
               State('choose_update', 'value')],
              prevent_initial_call=True)
def test(n_clicks, username, password, captcha, choose_update):
    if n_clicks:
        if password != '' and password is not None and captcha != '' and captcha is not None:
            down_data.setPassword(password)
            down_data.setUsername(username)
            down_data.setCaptcha(captcha)
            if choose_update == '更新':
                try:
                    down_data.catchDataUpdate()
                    return html.Div(['数据更新完毕'])
                except Exception as e:
                    print(e)
                    return html.Div(['用户名、密码错误或验证码输入有误！！'])
            try:
                down_data.catchDataUpdate()
                return html.Div(['下载数据完毕'])
            except:
                return html.Div(['用户名、密码错误或验证码输入有误！！'])
        return "请填写正确信息！！"


@app.callback(
    Output('message', 'children'),
    Input('image', 'n_clicks'),
    prevent_initial_call=True
)
def image(n_clicks):
    global down_data
    down_data = DownData('service', 'ts2049', 'WWOW')
    try:
        down_data.sessionCreate()
        return '验证码刷新完毕'
    except:
        return html.Div(['网络连接错误！！！'])
